﻿using DotNetNuke.Entities.Modules;
using DotNetNuke.Entities.Modules.Actions;
using DotNetNuke.Security;
using DotNetNuke.Services.Exceptions;
using DotNetNuke.Services.Localization;
using Spire.Doc;
using Spire.Doc.Documents;
using System;
using System.IO;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Christoc.Modules.SpireDNNWordExport
{
    public partial class View : SpireDNNWordExportModuleBase, IActionable
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!Page.IsPostBack)
                {
                    LoadPanes();
                }
            }
            catch (Exception exc)
            {
                Exceptions.ProcessModuleLoadException(this, exc);
            }
        }

        private void LoadPanes()
        {
            foreach (string pane in PortalSettings.ActiveTab.Panes)
            {
                Control obj = (Control)DotNetNuke.Common.Globals.FindControlRecursiveDown(Page, pane);
                PanesDropDownList.Items.Add(new ListItem(pane, obj.ClientID));
            }
        }

        private string BaseURL
        {
            get
            {
                string url = Request.Url.Authority;

                if (Request.ServerVariables["HTTPS"] == "on")
                {
                    url = "https://" + url;
                }
                else
                {
                    url = "http://" + url;
                }

                return url;
            }
        }

        public ModuleActionCollection ModuleActions
        {
            get
            {
                var actions = new ModuleActionCollection
                    {
                        {
                            GetNextActionID(), Localization.GetString("EditModule", LocalResourceFile), "", "", "",
                            EditUrl(), false, SecurityAccessLevel.Edit, true, false
                        }
                    };
                return actions;
            }
        }

        protected void WordsExportButton_Click(object sender, EventArgs e)
        {
            string pageSource = PageSourceHiddenField.Value;
            pageSource = "<html>" + pageSource.Replace("#g#", ">").Replace("#l#", "<") + "</html>";
            pageSource = pageSource.Replace("<div class=" + "\"exportButton\"" + ">", "<div class=" + "\"exportButton\"" + "style=" + "\"display: none\"" + ">");
            pageSource = pageSource.Replace("</head>", string.Format("<base href='{0}'></base></head>", BaseURL));

            MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(pageSource));
            StreamReader sr = new StreamReader(stream);
            Document doc = new Document();
            doc.LoadHTML(sr, XHTMLValidationType.None);
            doc.HtmlExportOptions.ImageEmbedded = true;
            string fileName = Guid.NewGuid().ToString() + ".docx";
            doc.SaveToFile(fileName, FileFormat.Docx, Response, HttpContentType.Attachment);

            Response.End();
        }
    }
}